﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SKIT.FlurlHttpClient.Wechat.Api;
using Volo.Abp;
using Volo.Abp.Modularity;

namespace Colorful.Furniture.MpManagement;

/// <summary>
///  微信公众号管理模块
/// </summary>
public class WechatMpModule : AbpModule
{
    /// <summary>
    ///  配置服务
    /// </summary>
    /// <param name="context"></param>
    /// <exception cref="UserFriendlyException"></exception>
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();

        var wechatMpSection = configuration.GetSection("WechatMp");

        if (!wechatMpSection.Exists())
            throw new UserFriendlyException("请在appsettings.json中配置WechatMp节点");

        Configure<WechatMpOptions>(wechatMpSection);

        var config = wechatMpSection.Get<WechatMpOptions>();

        context.Services.AddSingleton<IWechatMpExtensions, SkitWechatMpExtensions>();

        var wechatApiOptions = new WechatApiClientOptions
        {
            AppId = config.AppId,
            AppSecret = config.AppSecret,
            PushEncodingAESKey = config.EncodingAESKey,
            PushToken = config.Token
        };

        context.Services.AddTransient(x => new WechatApiClient(wechatApiOptions));
    }
}